package com.zpself.module.设计模式23.结构型模式.e装饰模式一咖啡厅.IO;

import org.junit.Test;

import java.io.*;
import java.util.zip.InflaterOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author By ZengPeng
 * @Description
 * @date in  2021/3/21 17:33
 * @Modified By
 */
public class FilterOutputStream分析 {
    final  String inFilePath = "C:\\Users\\ying\\zpFile\\toolInstall\\newSourceTree\\mayunResouce\\DesignPattern\\src\\main\\java\\com\\zpself\\module\\设计模式23\\结构型模式\\e装饰模式一咖啡厅\\IO\\1.txt";
    final  String path = "C:\\Users\\ying\\zpFile\\toolInstall\\newSourceTree\\mayunResouce\\DesignPattern\\src\\main\\java\\com\\zpself\\module\\设计模式23\\结构型模式\\e装饰模式一咖啡厅\\IO\\zipFile";
    final  String outFilePath = "C:\\Users\\ying\\zpFile\\toolInstall\\newSourceTree\\mayunResouce\\DesignPattern\\src\\main\\java\\com\\zpself\\module\\设计模式23\\结构型模式\\e装饰模式一咖啡厅\\IO\\2.zip";
    @Test
    public void test() throws IOException {
        try (   FileInputStream fis = new FileInputStream(inFilePath);
                FileOutputStream fos =new FileOutputStream(outFilePath)){
            //装饰模式
            //FilterOutputStream filter = new FilterOutputStream(null);
            //压缩
            //压缩(fis, fos);

            //解压
            解压();

//            b = new byte[1024];
//            int len ;
//            while ((len=fis.read(b))>-1){
//                fos.write(b,0,len);
//            }
//            fos.flush();
        }
    }



    private void 压缩(FileInputStream fis, FileOutputStream fos) throws IOException {
        ZipEntry zipEntry = new ZipEntry(inFilePath);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fos);
        zipOutputStream.putNextEntry(zipEntry);

        byte[] bytes = new byte[1024];
        int length;
        while((length = fis.read(bytes)) >= 0) {
            zipOutputStream.write(bytes, 0, length);
        }
        zipOutputStream.close();
    }

    private void 解压() throws IOException {
        //被解压的压缩文件
        //解压的目标目录
        File destDir = new File(path);

        byte[] buffer = new byte[1024];
        ZipInputStream zis = new ZipInputStream(new FileInputStream(outFilePath));
        //获取压缩包中的entry，并将其解压
        ZipEntry zipEntry = zis.getNextEntry();
        while (zipEntry != null) {
            File newFile = newFile(destDir, zipEntry);
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            //解压完成一个entry，再解压下一个
            zipEntry = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
    }

    //在解压目标文件夹，新建一个文件
    public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
        File destFile = new File(destinationDir, zipEntry.getName());

        String destDirPath = destinationDir.getCanonicalPath();
        String destFilePath = destFile.getCanonicalPath();

        if (!destFilePath.startsWith(destDirPath + File.separator)) {
            throw new IOException("该解压项在目标文件夹之外: " + zipEntry.getName());
        }
        return destFile;
    }
}
